#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <getopt.h>
#include <pthread.h>

#include "structs.h"
#include "modes.h"
#include "uglies.h"

#define SLEEP(secs) while(secs != 0) secs = sleep(secs)

char *strdup(const char *c);

extern struct mode modes[];

unsigned int num_commands = 0;
struct command **commands = NULL;

void *run_single_command(void *arg)
{
	int i = (int)arg;
	void * (*function)(void *) = modes[commands[i]->mode_num].pt2func;
	struct command *command = commands[i];

	while (1) 
	{
		time_t current_time = time(NULL);
		int time;

		function(command);
		time = difftime(command->wake, current_time);
		SLEEP(time);
	}
	return (void *)0;
}

void run_commands()
{
	int i;
	for (i = 0; i < num_commands; i++)
	{
		pthread_t t;
		pthread_create(
			&t, NULL,
			run_single_command,
			(void *)i);
	}
}

int main(int argc, char *argv[])
{
	parse_uglies();
	run_commands();

	while(1);

	return 0;
}
